$(function () {

    selMyTask(1);

});


//查询我发布的帮帮
function selMyTask(pg){
    $.ajax({
        type: 'post',
        url: ctx + "/selMyTask",   //提交到的url
        cache: false,
        data: {"id":user_id,"pg":pg},    //提交的参数
        dataType: 'json',
        success: function (data) {
            if (data.message=="fail") {
                $("#middle-myadd").children(".middle-data").css("display","none");
                $("#middle-myadd").children(".middle-page").css("display","none");
                $("#middle-myadd").children(".middle-nodata").css("display","block");
            }else{
                $("#middle-myadd").children(".middle-data").css("display","block");
                $("#middle-myadd").children(".middle-page").css("display","block");
                $("#middle-myadd").children(".middle-nodata").css("display","none");
                $("#middle-myadd").children(".middle-page").children(".count-page").text("共"+data.code+"页");
                var middleData = $("#middle-myadd").children(".middle-data");
                middleData.html("");
                $.each(data.list, function (index, item) {
                    var task = "<div class=\"data-box\">\n" +
                        "            <div class=\"task-id\">"+item.id+"</div>\n" +
                        "            <div class=\"data-title\"><span>"+item.title+"</span></div>\n" +
                        "            <div class=\"data-createtime\">"+item.createtime.substr(0,19)+"</div>\n" +
                        "            <div class=\"data-state\"></div>\n" +
                        "            <div class=\"data-operate\"></div>\n" +
                        "        </div>";
                    middleData.append(task);
                    if (item.usernum != 0){
                        $("#middle-myadd").children(".middle-data").children(".data-box:last").children(".data-state").text("已完成");
                        $("#middle-myadd").children(".middle-data").children(".data-box:last").children(".data-operate").html("<a href=\"\">详情</a>&nbsp;|&nbsp;<a href=\"\">删除</a>");
                    }else{
                        $("#middle-myadd").children(".middle-data").children(".data-box:last").children(".data-state").text("进行中");
                        $("#middle-myadd").children(".middle-data").children(".data-box:last").children(".data-operate").html("<a href=\"\">详情</a>&nbsp;|&nbsp;<a href=\"\">结账</a>");
                    }
                });
            }

        },
        error: function (data) {
            alert("查询失败！");
        }
    });
}


//查询我参与的帮帮
function selMyJoin(pg){
    $.ajax({
        type: 'post',
        url: ctx + "/selMyJoin",   //提交到的url
        cache: false,
        data: {"id":user_id,"pg":pg},    //提交的参数
        dataType: 'json',
        success: function (data) {
            if (data.message=="fail") {
                $("#middle-myjoin").children(".middle-data").css("display","none");
                $("#middle-myjoin").children(".middle-page").css("display","none");
                $("#middle-myjoin").children(".middle-nodata").css("display","block");
            }else{
                $("#middle-myjoin").children(".middle-data").css("display","block");
                $("#middle-myjoin").children(".middle-page").css("display","block");
                $("#middle-myjoin").children(".middle-nodata").css("display","none");
                $("#middle-myjoin").children(".middle-page").children(".count-page").text("共"+data.code+"页");
                if (data.code == 1){
                    $("#middle-myjoin").children(".middle-page").children(".next-page").css("cursor","not-allowed");
                }
                var middleData = $("#middle-myjoin").children(".middle-data");
                middleData.html("");
                $.each(data.list, function (index, item) {
                    var task = "<div class=\"data-box\">\n" +
                        "            <div class=\"task-id\">"+item.id+"</div>\n" +
                        "            <div class=\"data-title\"><span>"+item.title+"</span></div>\n" +
                        "            <div class=\"data-finishtime\">"+item.endtime.substr(0,19)+"</div>\n" +
                        "            <div class=\"data-user\"><span>"+item.user.nickname+"</span></div>\n" +
                        "            <div class=\"data-operate\"><a href=\"\">详情</a>&nbsp;|&nbsp;<a href=\"\">放弃</a></div>\n" +
                        "        </div>";
                    middleData.append(task);
                });
            }
        },
        error: function (data) {
            alert("查询失败！");
        }
    });
}


//查询我完成的帮帮
function selMyFinish(pg){
    $.ajax({
        type: 'post',
        url: ctx + "/selMyFinish",   //提交到的url
        cache: false,
        data: {"id":user_id,"pg":pg},    //提交的参数
        dataType: 'json',
        success: function (data) {
            if (data.message=="fail") {
                $("#middle-myfinish").children(".middle-data").css("display","none");
                $("#middle-myfinish").children(".middle-page").css("display","none");
                $("#middle-myfinish").children(".middle-nodata").css("display","block");
            }else{
                $("#middle-myfinish").children(".middle-data").css("display","block");
                $("#middle-myfinish").children(".middle-page").css("display","block");
                $("#middle-myfinish").children(".middle-nodata").css("display","none");
                $("#middle-myfinish").children(".middle-page").children(".count-page").text("共"+data.code+"页");
                if (data.code == 1){
                    $("#middle-myfinish").children(".middle-page").children(".next-page").css("cursor","not-allowed");
                }
                var middleData = $("#middle-myfinish").children(".middle-data");
                middleData.html("");
                $.each(data.list, function (index, item) {
                    var task = "<div class=\"data-box\">\n" +
                        "            <div class=\"task-id\">"+item.taskid+"</div>\n" +
                        "            <div class=\"data-title\"><span>"+item.task.title+"</span></div>\n" +
                        "            <div class=\"data-finishtime\">"+item.finishtime.substr(0,19)+"</div>\n" +
                        "            <div class=\"data-user\"><span>"+item.user.nickname+"</span></div>\n" +
                        "            <div class=\"data-operate\"><a href=\"\">详情</a>&nbsp;|&nbsp;<a href=\"\">删除</a>\n" +
                        "            </div>\n" +
                        "        </div>";
                    middleData.append(task);
                });
            }
        },
        error: function (data) {
            alert("查询失败！");
        }
    });
}


//查询我的评论帮帮
function selMyComment(pg){
    $.ajax({
        type: 'post',
        url: ctx + "/selMyComment",   //提交到的url
        cache: false,
        data: {"id":user_id,"pg":pg},    //提交的参数
        dataType: 'json',
        success: function (data) {
            if (data.message=="fail") {
                $("#middle-mycomment").children(".middle-data").css("display","none");
                $("#middle-mycomment").children(".middle-page").css("display","none");
                $("#middle-mycomment").children(".middle-nodata").css("display","block");
            }else{
                $("#middle-mycomment").children(".middle-data").css("display","block");
                $("#middle-mycomment").children(".middle-page").css("display","block");
                $("#middle-mycomment").children(".middle-nodata").css("display","none");
                $("#middle-mycomment").children(".middle-page").children(".count-page").text("共"+data.code+"页");
                if (data.code == 1){
                    $("#middle-mycomment").children(".middle-page").children(".next-page").css("cursor","not-allowed");
                }
                var middleData = $("#middle-mycomment").children(".middle-data");
                middleData.html("");
                $.each(data.list, function (index, item) {
                    var task = "<div class=\"data-box\">\n" +
                        "            <div class=\"task-id\">"+item.task.id+"</div>\n" +
                        "            <div class=\"data-content\"><span>"+item.content+"</span></div>\n" +
                        "            <div class=\"data-createtime\">"+item.time.substr(0,19)+"</div>\n" +
                        "            <div class=\"data-operate\"><a href=\"\">详情</a>&nbsp;|&nbsp;<a href=\"\">删除</a>\n" +
                        "            </div>\n" +
                        "        </div>";
                    middleData.append(task);
                });
            }
        },
        error: function (data) {
            alert("查询失败！");
        }
    });
}


//我的帮帮nav切换
function topNavClick(op) {
    $(".now-page").text(1);
    var c = $(op).attr("class");
    if (c == "top-nav") {
        $("#top").children("div").attr("class", "top-nav");
        $(op).attr("class", "top-nav-now");
    }
    var text = $(op).text();
    if (text == "我发布的") {
        $("#middle-myadd").css("display", "block");
        $("#middle-myjoin").css("display", "none");
        $("#middle-myfinish").css("display", "none");
        $("#middle-mycomment").css("display", "none");
        selMyTask(1);
    } else if (text == "我参与的") {
        $("#middle-myadd").css("display", "none");
        $("#middle-myjoin").css("display", "block");
        $("#middle-myfinish").css("display", "none");
        $("#middle-mycomment").css("display", "none");
        selMyJoin(1);
    } else if (text == "我完成的") {
        $("#middle-myadd").css("display", "none");
        $("#middle-myjoin").css("display", "none");
        $("#middle-myfinish").css("display", "block");
        $("#middle-mycomment").css("display", "none");
        selMyFinish(1);
    } else if (text == "我的评论") {
        $("#middle-myadd").css("display", "none");
        $("#middle-myjoin").css("display", "none");
        $("#middle-myfinish").css("display", "none");
        $("#middle-mycomment").css("display", "block");
        selMyComment(1);
    }
}


//页码翻页
function prevNextPage(op) {
    var text = $(op).text().trim();
    var txt = $("#top").children(".top-nav-now").text().trim();
    if (text == "<") {
        var pg = Number($(op).next().text());
        var length = $(op).next().next().text().length;
        var countpg = Number($(op).next().next().text().substr(1, 2));
        if(pg != 1){
            $(op).next().next().css("cursor","pointer");
            $(op).next().text(pg-1);
            if(txt == "我发布的"){
                selMyTask(pg-1);
            }else if(txt == "我参与的"){
                selMyJoin(pg-1);
            }else if(txt == "我完成的"){

            }else if(txt == "我的评论"){

            }
        }
        if(pg == 2){
            $(op).css("cursor","not-allowed");
        }
    } else {
        var pg = Number($(op).prev().text());
        var length = $(op).next().text().length;
        var countpg = Number($(op).next().text().substr(1, length-2));
        if(pg != countpg){
            $(op).prev().prev().css("cursor","pointer");
            $(op).prev().text(pg+1);
            if(txt == "我发布的"){
                selMyTask(pg+1);
            }else if(txt == "我参与的"){
                selMyJoin(pg+1);
            }else if(txt == "我完成的"){

            }else if(txt == "我的评论"){

            }
        }
        if(pg == countpg-1){
            $(op).css("cursor","not-allowed");
        }
    }
}